import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import Ability from '@ohos.app.ability.Ability';

async function enterImmersion(windowClass: window.Window) {
  // 获取状态栏和导航栏的高度
  windowClass.on("avoidAreaChange", ({ type, area }) => {
    if (type == window.AvoidAreaType.TYPE_SYSTEM) {
      // 将状态栏和导航栏的高度保存在AppStorage中
      AppStorage.SetOrCreate<number>("topHeight", area.topRect.height);
      AppStorage.SetOrCreate<number>("bottomHeight", area.bottomRect.height);
    }
  })
  // 设置窗口布局为沉浸式布局
  await windowClass.setWindowLayoutFullScreen(true)
  await windowClass.setWindowSystemBarEnable(["status", "navigation"])
  // 设置状态栏和导航栏的背景为透明
  await windowClass.setWindowSystemBarProperties({
    navigationBarColor: "#00000000",
    statusBarColor: "#00000000",
    navigationBarContentColor: "#FF0000",
    statusBarContentColor: "#FF0000"
  })
}
export default class EntryAbility extends UIAbility {
  onCreate(want, launchParam) {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  }

  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  async onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    // let windowClass:window.Window = await windowStage.getMainWindow()
    // await enterImmersion(windowClass)

    // 1.获取应用主窗口。
    // let windowClass = null;
    // windowStage.getMainWindow((err, data) => {
    //
    //   if (err.code) {
    //     console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err));
    //     return;
    //   }
    //
    //   windowClass = data;
    //   console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
    //
    //
    //   // 2.实现沉浸式效果：设置导航栏、状态栏不显示。
    //   let names = [];
    //   windowClass.setWindowSystemBarEnable(names, (err) => {
    //     if (err.code) {
    //       console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err));
    //       return;
    //     }
    //     console.info('Succeeded in setting the system bar to be visible.');
    //   });
    //
    //
    //   windowClass.setWindowLayoutFullScreen(true)
    // })
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
    windowStage.loadContent('pages/Index', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }

}
